<template>
  <el-container class="layout-container">
    <!-- 左侧菜单 -->
    <el-aside :width="isCollapsed ? '60px' : '200px'" class="sidebar">
      <div class="el-aside__logo"></div>
      <el-menu
        :default-active="$route.path"
        active-text-color="#ffffff"
        background-color="transparent"
        text-color="#d9e0e7"
        router
        :collapse="isCollapsed"
        class="sidebar-menu"
      >
        <el-menu-item index="/admin/dashboard">
          <el-icon><Management /></el-icon>
          <span>仪表盘</span>
        </el-menu-item>
        
        <el-menu-item index="/index">
          <el-icon><Link /></el-icon>
          <span>返回首页</span>
        </el-menu-item>

        <!-- 审核中心 -->
        <el-sub-menu index="audit">
          <template #title>
            <el-icon><Promotion /></el-icon>
            <span>审核中心</span>
          </template>
          <el-menu-item index="/admin/spu">
            <el-icon><Goods /></el-icon>
            <span>商品审核</span>
          </el-menu-item>
          <el-menu-item index="/admin/merchant">
            <el-icon><User /></el-icon>
            <span>商户审核</span>
          </el-menu-item>
          <el-menu-item index="/admin/comment">
            <el-icon><EditPen /></el-icon>
            <span>评论审核</span>
          </el-menu-item>
        </el-sub-menu>

        <!-- 用户管理 -->
        <el-menu-item index="/admin/user">
          <el-icon><UserFilled /></el-icon>
          <span>用户管理</span>
        </el-menu-item>

        <el-menu-item index="/admin/category">
          <el-icon><CollectionTag /></el-icon>
          <span>分类管理</span>
        </el-menu-item>

        <!-- 促销活动 -->
        <el-menu-item index="/admin/promotion">
          <el-icon><Crop /></el-icon>
          <span>促销活动管理</span>
        </el-menu-item>

      <el-menu-item index="/admin/promotionCon">
          <el-icon><Crop /></el-icon>
          <span>促销活动配置</span>
        </el-menu-item>

          <el-menu-item index="/admin/notice">
          <el-icon><Postcard /></el-icon>
          <span>站内信管理</span>
        </el-menu-item>
      </el-menu>
    </el-aside>

    <!-- 右侧主区域 -->
    <el-container>
      <!-- 顶部 -->
      <el-header class="header">
        <div class="header-left">
          <el-icon class="toggle-icon" @click="isCollapsed = !isCollapsed">
            <Menu />
          </el-icon>
          <span class="welcome-text">管理员：<strong>{{ adminName }}</strong></span>
        </div>
        <el-dropdown placement="bottom-end" @command="handleCommand">
          <span class="el-dropdown__box">
            <el-avatar :src="avatar" class="avatar" />
            <el-icon><CaretBottom /></el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="logout" :icon="SwitchButton">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </el-header>

      <!-- 主内容 -->
      <el-main class="main-content">
        <router-view />
      </el-main>

      <!-- 底部 -->
      <el-footer class="footer">Admin Console ©2025</el-footer>
    </el-container>
  </el-container>
</template>

<script setup>
import {
  CaretBottom,
  CollectionTag,
  Crop,
  EditPen,
  Goods,
  Link,
  Management,
  Menu,
  Postcard,
  Promotion,
  SwitchButton,
  User,
  UserFilled
} from '@element-plus/icons-vue'
import avatar from '@/assets/default.png'
import { ElMessageBox, ElMessage } from 'element-plus'
import { ref } from 'vue'
import { useRouter } from 'vue-router'

// 示例管理员名
const adminName = 'Admin'

const router = useRouter()
const isCollapsed = ref(false)

const handleCommand = (command) => {
  if (command === 'logout') {
    ElMessageBox.confirm('确认退出登录吗？', '提示', {
      type: 'warning'
    }).then(() => {
      // TODO: 清除 token / store
      ElMessage.success('已退出')
      router.push('/login')
    })
  }
}
</script>

<style scoped>
.layout-container {
  height: 100vh;
  overflow: hidden;
  display: flex;
}

.sidebar {
  background: var(--sidebar-bg, #304156);
  transition: width 0.2s ease;
  box-shadow: 2px 0 6px rgba(0, 0, 0, 0.1);

  &__logo {
    height: 48px;
    margin: 12px 0;
    /* background: url('@/assets/login_title.png') no-repeat center / 110px auto; */
    transition: var(--transition);
  }

  .sidebar-menu {
    border-right: none;
    height: calc(100vh - 72px);
    overflow: hidden;
  }
}

.header {
  background-color: var(--header-bg, #ffffff);
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
  padding: 0 16px;
  height: 56px;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 10px;

  .toggle-icon {
    font-size: 20px;
    cursor: pointer;
    color: var(--text-secondary, #606266);
    transition: var(--transition);
    &:hover {
      color: var(--primary-color, #409eff);
      transform: rotate(90deg);
    }
  }

  .welcome-text {
    font-size: 14px;
    color: var(--text-color, #303133);
    strong {
      font-weight: 600;
    }
  }
}

.el-dropdown__box {
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;

  .avatar {
    width: 32px;
    height: 32px;
    transition: var(--transition);
    &:hover {
      transform: scale(1.05);
      border: 1px solid var(--primary-color, #409eff);
    }
  }

  .el-icon {
    color: var(--text-secondary, #606266);
    font-size: 14px;
    transition: var(--transition);
    &:hover {
      color: var(--primary-color, #409eff);
    }
  }
}

.main-content {
  padding: 0;
  height: calc(100vh - 112px);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.footer {
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  color: var(--text-secondary, #606266);
  background-color: var(--header-bg, #ffffff);
  box-shadow: 0 -1px 4px rgba(0, 0, 0, 0.08);
}
</style>
